/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.core.view.mbean;

import com.brain.core.common.UtilCore;
import com.brain.core.configuracion.WebServletContextListener;
import com.brain.core.dto.EmpresaDto;
import com.brain.core.dto.UbigeoDto;
import com.brain.core.exception.DAOException;
import com.brain.core.manager.BitacoraManager;
import com.brain.core.manager.EmpresaManager;
import com.brain.core.manager.RecursosManager;
import com.brain.core.manager.UbigeoManager;
import com.brain.core.dto.EmpresaUbigeoDto;
import com.brain.core.manager.EmpresaUbigeoManager;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Indra
 */
@Controller("ubigeoMBean")
@Scope("session")
public class UbigeoMBean extends GenericMBean implements Serializable {

    private static Logger logger = Logger.getLogger(UbigeoMBean.class);
    @Resource
    private UbigeoManager manager;
    @Resource
    private EmpresaManager empresaManager;
    @Resource
    private BitacoraManager bitacoraManager;
    @Resource
    private EmpresaUbigeoManager empresaUbigeoManager;
    @Resource
    private RecursosManager recursosManager;
    private List<UbigeoDto> lista;
    private UbigeoDto dto;
    private String keyEntidad = "key.entidad.ubigeo";
    private TreeNode ubigeos;
    private TreeNode selectedUbigeo;
    private UbigeoDto ubigeoPrincipal;
    private UbigeoDto ubigeoSeleccionadoDto;
    private EmpresaDto empresaId;
    private Long verificarCargaUbigeo;
    private Boolean validarBotonGuardar = Boolean.FALSE;
    private Boolean tipoDeUsuario = Boolean.TRUE;
    @Autowired
    private ApplicationMBean applicationMBean;
    @Autowired
    private SessionMBean sessionMBean;
    private List<EmpresaDto> listaEmpresa;
    private List<EmpresaUbigeoDto> listaEmpresaUbigeo;

    public UbigeoMBean() {
    }

    public String iniciar() {
        try {
            this.lista = null;
            this.dto = null;
            this.empresaId = new EmpresaDto();
            /*
             if(this.manager.obtenerEmpresaIdSessionUsuario()!=null){
                 
             this.tipoDeUsuario = Boolean.TRUE;
             this.empresaId = this.empresaManager.obtenerPorId(this.manager.obtenerEmpresaIdSessionUsuario());
             this.listaEmpresaUbigeo = this.empresaUbigeoManager.obtenerTodosPorEmpresa(this.empresaId); 
             this.ubigeos = this.manager.cargarUbigeosUsuarioAdministrador(this.listaEmpresaUbigeo);
             
             }else{
                
             this.tipoDeUsuario = Boolean.FALSE;
             this.empresaId = new EmpresaDto(this.manager.getIdUbigeoEmpresaPrincipal());
             this.ubigeos = this.cargarUbigeos(this.manager.obtenerPorId(this.manager.getIdUbigeoPrincipal()));

             }*/
            this.ubigeos = this.cargarUbigeos(this.manager.obtenerPorId(this.manager.getIdUbigeoPrincipal()));


        } catch (DAOException ex) {
            showError("ERROR_SISTEMA", ex.getMessage());
        }

        return manager.getUrlLista();
    }

    public String iniciarUbigeoEmpresa(EmpresaDto empresaDto) {
        try {
            this.lista = null;
            this.dto = null;

            this.lista = this.manager.obtenerPaisesActivos();
            this.listaEmpresaUbigeo = this.empresaUbigeoManager.obtenerTodosPorEmpresa(empresaDto);

            this.ubigeos = this.manager.cargarProcesoInicialDesdeEmpresa(this.listaEmpresaUbigeo);



        } catch (DAOException ex) {
            showError("ERROR_SISTEMA", ex.getMessage());
        }

        return manager.getUrlLista();
    }

    public void filtroEmpresa() {
        try {
            if (this.empresaId != null) {
                this.listaEmpresaUbigeo = this.empresaUbigeoManager.obtenerTodosPorEmpresa(this.empresaId);
                this.ubigeos = this.manager.cargarUbigeosUsuarioAdministrador(this.listaEmpresaUbigeo);
            } else {
                this.empresaId = new EmpresaDto(this.manager.getIdUbigeoEmpresaPrincipal());
                this.ubigeos = this.cargarUbigeos(this.manager.obtenerPorId(this.manager.getIdUbigeoPrincipal()));
            }

        } catch (DAOException ex) {
            showError("ERROR_SISTEMA", ex.getMessage());
        }
    }

    public String gestionFeriados() {
        this.ubigeoSeleccionadoDto = (UbigeoDto) selectedUbigeo.getData();
        if (this.ubigeoSeleccionadoDto.getUbigeoPadreDto().getId() == 0) {
            FeriadoPaisMBean feriadoPaisMBean = (FeriadoPaisMBean) WebServletContextListener.getApplicationContext().getBean("feriadoPaisMBean");
            return feriadoPaisMBean.iniciar(ubigeoSeleccionadoDto);
        } else {
            recursosManager.showWarning(UtilCore.Internacionalizacion.getMensajeInternacional("ubigeo.mensaje.no.feriados"));
        }
        return "";
    }

    private TreeNode cargarUbigeos(UbigeoDto ubigeoPrincipal) {
        return this.manager.ubigeosCarga(ubigeoPrincipal);
    }

    private TreeNode cargarUbigeosPorEmpresa(UbigeoDto ubigeoPrincipal, List<EmpresaUbigeoDto> listaEmpresaUbigeo) {
        return this.manager.ubigeosCarga(ubigeoPrincipal);
    }

    public void editarUbigeo() {
        this.ubigeoSeleccionadoDto = (UbigeoDto) selectedUbigeo.getData();
        this.validarBotonGuardar = this.manager.validarEdicionTipo(this.ubigeoSeleccionadoDto.getUbigeoPadreDto());

        this.dto = this.ubigeoSeleccionadoDto;
        sessionMBean.setAccion(applicationMBean.getEditar());
    }

    public void nuevaUbigeo() {
        this.ubigeoSeleccionadoDto = (UbigeoDto) selectedUbigeo.getData();

        this.dto = new UbigeoDto();
        this.dto.setEstado(Boolean.TRUE);
        this.dto.setUsuarioDto(sessionMBean.getSessionUsuarioDto());
        this.dto.setFecha(UtilCore.Fecha.obtenerFechaActualDate());
        this.dto.setUbigeoPadreDto(this.ubigeoSeleccionadoDto);
        this.dto.setRetencion(BigDecimal.ZERO);
        this.dto.setRetencionHonorarios(BigDecimal.ZERO);
        this.dto.setTipo(this.manager.evaluarTipo(this.ubigeoSeleccionadoDto));

        validarBotonGuardar = Boolean.FALSE;

        sessionMBean.setAccion(applicationMBean.getNuevo());
    }

    public void guardar() {
        manager.guardar(this.dto);
        this.filtroEmpresa();
    }

    public String verBitacora() {
        UbigeoDto ubgDtoAux = new UbigeoDto();
        ubgDtoAux = (UbigeoDto) selectedUbigeo.getData();
        bitacoraManager.cargarBitacoraEntidad(manager.getIdEntidad(), ubgDtoAux.getId());
        return null;
    }

    public ApplicationMBean getApplicationMBean() {
        return applicationMBean;
    }

    public void setApplicationMBean(ApplicationMBean applicationMBean) {
        this.applicationMBean = applicationMBean;
    }

    public UbigeoDto getDto() {
        return dto;
    }

    public void setDto(UbigeoDto dto) {
        this.dto = dto;
    }

    public String getKeyEntidad() {
        return keyEntidad;
    }

    public void setKeyEntidad(String keyEntidad) {
        this.keyEntidad = keyEntidad;
    }

    public List<UbigeoDto> getLista() {
        return lista;
    }

    public void setLista(List<UbigeoDto> lista) {
        this.lista = lista;
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        UbigeoMBean.logger = logger;
    }

    public UbigeoManager getManager() {
        return manager;
    }

    public void setManager(UbigeoManager manager) {
        this.manager = manager;
    }

    public RecursosManager getRecursosManager() {
        return recursosManager;
    }

    public void setRecursosManager(RecursosManager recursosManager) {
        this.recursosManager = recursosManager;
    }

    public TreeNode getSelectedUbigeo() {
        return selectedUbigeo;
    }

    public void setSelectedUbigeo(TreeNode selectedUbigeo) {
        this.selectedUbigeo = selectedUbigeo;
    }

    public SessionMBean getSessionMBean() {
        return sessionMBean;
    }

    public void setSessionMBean(SessionMBean sessionMBean) {
        this.sessionMBean = sessionMBean;
    }

    public UbigeoDto getUbigeoPrincipal() {
        return ubigeoPrincipal;
    }

    public void setUbigeoPrincipal(UbigeoDto ubigeoPrincipal) {
        this.ubigeoPrincipal = ubigeoPrincipal;
    }

    public UbigeoDto getUbigeoSeleccionadoDto() {
        return ubigeoSeleccionadoDto;
    }

    public void setUbigeoSeleccionadoDto(UbigeoDto ubigeoSeleccionadoDto) {
        this.ubigeoSeleccionadoDto = ubigeoSeleccionadoDto;
    }

    public TreeNode getUbigeos() {
        return ubigeos;
    }

    public void setUbigeos(TreeNode ubigeos) {
        this.ubigeos = ubigeos;
    }

    public List<EmpresaDto> getListaEmpresa() {
        return this.recursosManager.obtenerEmpresasActivasCombo();
    }

    public void setListaEmpresa(List<EmpresaDto> listaEmpresa) {
        this.listaEmpresa = listaEmpresa;
    }

    public EmpresaDto getEmpresaId() {
        return empresaId;
    }

    public void setEmpresaId(EmpresaDto empresaId) {
        this.empresaId = empresaId;
    }

    public Long getVerificarCargaUbigeo() {
        return verificarCargaUbigeo;
    }

    public void setVerificarCargaUbigeo(Long verificarCargaUbigeo) {
        this.verificarCargaUbigeo = verificarCargaUbigeo;
    }

    public Boolean getValidarBotonGuardar() {
        return validarBotonGuardar;
    }

    public void setValidarBotonGuardar(Boolean validarBotonGuardar) {
        this.validarBotonGuardar = validarBotonGuardar;
    }

    public List<EmpresaUbigeoDto> getListaEmpresaUbigeo() {
        return listaEmpresaUbigeo;
    }

    public void setListaEmpresaUbigeo(List<EmpresaUbigeoDto> listaEmpresaUbigeo) {
        this.listaEmpresaUbigeo = listaEmpresaUbigeo;
    }

    public Boolean getTipoDeUsuario() {
        return tipoDeUsuario;
    }

    public void setTipoDeUsuario(Boolean tipoDeUsuario) {
        this.tipoDeUsuario = tipoDeUsuario;
    }
}
